package fhr;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName DBManager
 * @Description 数据库操作工具类
 * @author gongjing
 * @date 2021年6月4日
 */
public class SQL {
	// public String DriverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	public String DriverName = "com.mysql.jdbc.Driver";
	public String URL = "jdbc:mysql://localhost:3306/"; // 端口3306 MySQL数据库的驱动名称com.mysql.jdbc.Driver
	public String DATABASE = "login";
	public String USERNAME = "root";
	public String PASSWORD = "123456";
	public Connection conn;
	private Statement stmt = null;
	private ResultSet rs = null;
	public String dateTime;

	public SQL() {
	}

	public SQL(String drivername, String url, String databasename, String username, String password) {
		this.DriverName = drivername;
		this.URL = url;
		this.DATABASE = databasename;
		this.USERNAME = username;
		this.PASSWORD = password;
	}

	/**
	 * @Title: getConnection @Description: 获得Connection对象 @return
	 *         返回Connection对象 @throws
	 */
	public Connection getConnection() {
		try {
			// 1.加载SQL Server驱动
			Class.forName(DriverName);
			// 2.建立数据库连接
			this.conn = DriverManager.getConnection(URL + DATABASE, USERNAME, PASSWORD);
			System.out.println("数据库连接成功！");
		} catch (ClassNotFoundException e) {
			System.out.println("驱动加载异常：" + e.getMessage());
		} catch (SQLException e) {
			System.out.println("获取数据库连接异常：" + e.getMessage());
		}
		return conn;
	}

	/**
	 * @Title: getStatement @Description: 产生Statement对象 @return
	 *         返回Statement对象 @throws
	 */
	public Statement getStatement() {
		stmt = null;
		try {
			if (conn == null) {
				conn = getConnection();
			}
			stmt = conn.createStatement();
		} catch (Exception e) {
			System.out.println("产生Statement对象失败" + e.getMessage());
		}
		return stmt;
	}

	/**
	 * @Title: excuteQuery @Description: 查询数据库，获得结果数据集 @param sql语句 @return
	 *         返回ResultSet数据集 @throws
	 */
	public ResultSet excuteQuery(String sql) {
		rs = null;
		try {
			stmt = this.getStatement();
			rs = stmt.executeQuery(sql);
			return rs;
		} catch (SQLException e) {
			System.out.println("数据库查询出错！" + e.getMessage());
			return null;
		}
	}

	/**
	 * @Title: excuteNonQuery @Description: 执行插入、更新、删除操作 @param @param sql语句 @return
	 *         返回受影响的行数 @throws
	 */
	public int excuteNonQuery(String sql) {
		int rowcount = 0;
		try {
			stmt = this.getStatement();
			rowcount = stmt.executeUpdate(sql);
			System.out.println("该操作影响了" + rowcount + "行！");
			if (!conn.getAutoCommit()) {
				conn.commit();
			}
			return rowcount;
		} catch (Exception e) {
			System.out.println("数据库更新出错！" + e.getMessage());
			return 0;
		}

	}

	/**
	 * @Title: Release
	 * @Description: 释放资源
	 * @throws
	 */
	public void Release() {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (stmt != null) {
				stmt = null;
			}
			if (conn != null) {
				conn = null;
			}
		} catch (SQLException e) {
			System.out.println("数据库资源正在占用中，无法释放！" + e.getMessage());
		}
	}

	public String getdateTime() {
		Date time = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateTime = df.format(time);
		System.out.println(dateTime);
		return dateTime;
	}
}
